/**

 @Name：CLTPHP 会员
 @Author：chichu
 @Site：http://www.cltphp.com/admin/
 @License：LPPL
 */
layui.define(['admin', 'table', 'util', 'form'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element, form = layui.form;
    //全部数据
    tableIn = table.render({
        elem: '#list',
        url: '/shengteng/userLevel/index',
        toolbar: '#toolbar',
        method: 'post',
        cols: [[
            {field: 'level_id', title: 'ID', width: 80, fixed: true, sort: true},
            {field: 'level_name', title: '名称', width: 120,edit:'text'},
            {field: 'bomlimit', title: '满足积分条件', width: 150,templet:'#bom'},
            {field: 'sort',align: 'center',title: '排序', width: 120,templet:'#sort',sort: true,edit:'text'},
            {field: 'create_time', title: '添加时间', width: 180,hide:true},
            {field: 'update_time', title: '更新时间', width: 180,hide:true},
            {width: 160, align: 'center', toolbar: '#action'}
        ]]
    });
    //监听工具条
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var level_id = data.level_id;
        if (obj.event === 'del') {
            layer.confirm('您确定要删除该会员组吗？', function () {
                admin.req({
                    url: '/shengteng/userLevel/del/'
                    , type: 'post'
                    , data: {'level_id': level_id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        } else if (obj.event === 'edit') {
            var index = layer.open({
                type: 2,
                content: '/shengteng/userLevel/edit/level_id/' + level_id,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }
    });
    //编辑提交
    form.on('submit(edit-btn)', function (data) {
        admin.req({
            url: '/shengteng/userLevel/edit/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });

    table.on('edit(list)', function(obj){
        var param={};
        param[obj.field] = obj.value;
        param['level_id'] = obj.data.level_id;
        admin.req({
            url: '/shengteng/userLevel/cf'
            ,data: param
            ,type:'post'
            , success: function (res) {
                if(res.code==0){
                    layer.msg(res.msg,{time:1000,icon:1});
                }
                tableIn.reload();
            }
        });
    });

    //头工具栏事件
    table.on('toolbar(list)', function(obj){
        switch(obj.event){
            case 'add':
                var index = layer.open({
                    type: 2,
                    content: '/shengteng/userLevel/add/',
                    area: ['300px', '300px'],
                    maxmin: true
                });
                layer.full(index);
                break;
        }
    });
    //添加提交
    form.on('submit(add-btn)', function(data){
        admin.req({
            url: '/shengteng/userLevel/add/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });
    exports('userLevel', {});
});
